import Vue from 'vue'
import Router from 'vue-router'

// 处理路由跳转报错
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

// 判断设备来加载文件
import { isPad, isPc, isMobile } from '../utils'
let folderName = 'app'
setImmediate(() => {

  if(isPc()) {
    folderName = 'pc'
  }
  if(isPad() || isMobile()) {
    folderName = 'app'
  }
})

export default new Router({
  mode: 'hash',
  routes: [
    {
      path: '/works/:id',
      name: 'Works',
      component: (resolve) => require([`@/views/${folderName}/works`], resolve)
    },
    {
      path: '/goods/:id',
      name: 'Goods',
      component: (resolve) => require([`@/views/${folderName}/goods`], resolve)
    },
    {
      path: '/aerialPhotography/:panoId',
      name: 'AerialPhotography',
      component: (resolve) => require([`@/views/${folderName}/aerialPhotography`], resolve)
    },
    {
      path: '/overview/:id',
      name: 'Overview',
      component: (resolve) => require([`@/views/${folderName}/ovv`], resolve)
    },
    {
      path: '/tourview/:id',
      name: 'Tourview',
      component: (resolve) => require([`@/views/${folderName}/tourview`], resolve)
    },
    {
      path: '/handdraw/:id',
      name: 'Handdraw',
      component: (resolve) => require([`@/views/${folderName}/handdraw`], resolve)
    },
    {
      path: '/signature',
      name: 'Signature',
      component: (resolve) => require([`@/views/wechat/signature`], resolve),
      props: (route) => ({ id: route.query.id })
    },
    {
      path: '/permission',
      name: 'Permission',
      component: (resolve) => require(['@/views/permission'], resolve),
      props: (route) => ({ id: route.query.id })
    },
    {
      path: '/panoview/:panoId/invalid',
      name: 'Invalid',
      component: (resolve) => require(['@/views/404'], resolve)
    },
    {
      path: '/subscribe/:panoId',
      name: 'Subscribe',
      component: (resolve) => require(['@/views/app/subscribe'], resolve)
    },
    {
      path: '/panoview/:panoId',
      name: 'Home',
      component: (resolve) => require([`@/views/${folderName}`], resolve),
      props: (route) => ({ ismaster: route.query.ismaster }),
      children: [{
          path: '/panoview/:panoId/login',
          name: 'Login',
          component: (resolve) => require([`@/views/${folderName}/entry`], resolve)
        },
        {
          path: '/panoview/:panoId/goods-detail',
          name: 'GoodsDetail',
          component: (resolve) => require([`@/views/${folderName}/goodsDetail`], resolve),
          props: (route) => ({ id: route.query.id })
        },
        {
          path: '/panoview/:panoId/cart',
          name: 'Cart',
          component: (resolve) => require([`@/views/${folderName}/cart`], resolve)
        },
        {
          path: '/panoview/:panoId/scenepay',
          name: 'ScenePay',
          component: (resolve) => require([`@/views/${folderName}/scenePay`], resolve),
          props: (route) => ({ id: route.query.id })
        },
        {
          path: '/panoview/:panoId/cardlist',
          name: 'CardList',
          component: resolve => require([`@/views/wechat/card`], resolve)
        },
      ]
    }
  ]
})
